import { ref } from 'vue';
import { Input, message } from 'ant-design-vue';
import { useAuthStore } from '@/store/modules/auth';
import { isNonNegativeInteger } from '@/utils';
import { machineNoList } from './machineNoList';
let topFormModel;
let bottomFormModel;
const lotType = ref('input');
const lotids = ref([]);
const moduleNoSearchData = ref(null);
const authStore = useAuthStore();
const inkTypeis = ref('input');
const inkTypeList = ref([]);
const printMode = ref(0);

const printModeList = ref([
  {
    value: '精细模式',
    label: '精细模式'
  },
  {
    value: '超精细薄',
    label: '超精细薄'
  },
  {
    value: '超精细中',
    label: '超精细中'
  },
  {
    value: '超精细厚',
    label: '超精细厚'
  }
]);

export default {
  url: '/qms/agv/word/produce/list',
  addUrl: '/qms/agv/word/produce/insert',
  editUrl: '/qms/agv/word/produce/update',
  delUrl: '/qms/agv/word/produce/deleteBatch',
  modalGetTableDataUrl: '/qms/agv/word/produce/',
  delMethod: 'put',
  searchFormList: [
    {
      type: 'input',
      key: 'prodNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6
    },
    {
      type: 'input',
      key: 'lotId',
      label: 'LOT号',
      placeholder: '请输入LOT号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },

    {
      type: 'select',
      key: 'machineNo',
      label: '机台编号',
      placeholder: '请输入机台编号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      search: true,
      selectOptions: machineNoList
    },
    {
      type: 'select',
      key: 'shiftType',
      label: '班别',
      placeholder: '请选择班别',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      selectOptions: [
        {
          value: '0',
          label: '白班'
        },
        {
          value: '1',
          label: '夜班'
        }
      ]
    },
    {
      type: 'input',
      key: 'operator',
      label: '操作员',
      placeholder: '请输入操作员',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'dateRangePicker',
      key: 'time',
      label: '时间',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  searchParam: ref({
    prodNo: null,
    lotId: null,
    machineNo: null,
    operator: null,
    shiftType: null,
    state: null,
    time: null
  }),
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '型号版本',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'lotId',
      dataIndex: 'lotId',
      title: 'LOT号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'modifyTime',
      dataIndex: 'modifyTime',
      title: '时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'inkType',
      dataIndex: 'inkType',
      title: '油墨类型',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'inkColor',
      dataIndex: 'inkColor',
      title: '油墨颜色',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'faceNum',
      dataIndex: 'faceNum',
      title: '面数',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'faceType',
      dataIndex: 'faceType',
      title: '面类型',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'faceA',
      dataIndex: 'faceA',
      title: 'A面',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'faceB',
      dataIndex: 'faceB',
      title: 'B面',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'periods',
      dataIndex: 'periods',
      title: '周期',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'machineNo',
      dataIndex: 'machineNo',
      title: '机台编号',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'printMode',
      dataIndex: 'printMode',
      title: '打印模式',
      width: 140,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'checkType1',
      dataIndex: 'checkType1',
      title: '首件检查',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkType2',
      dataIndex: 'checkType2',
      title: '自主检查',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkType3',
      dataIndex: 'checkType3',
      title: '末件检查',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '操作员',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'shiftType',
      dataIndex: 'shiftType',
      title: '班别',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['白班', '夜班'][record.shiftType];
      }
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],
  scroll: { x: 2500, y: 'auto' },

  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },

    {
      type: 'searchInput',
      key: 'lotId',
      label: 'Lot号',
      span: 8,
      rule: {
        required: true,
        message: '请输入Lot号',
        trigger: 'change'
      },
      searchKey: 'lotId',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      disabled: false,
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
        bottomFormModel.value.shiftType = searchData.shiftType;
      }
    },

    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      span: 8,
      rule: {
        required: true,
        message: '请输入生产型号',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'datePicker',
      key: 'modifyTime',
      label: '时间',
      span: 8,
      rule: {
        required: true,
        message: '请选择时间',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },
    {
      type: 'select',
      key: 'machineNo',
      label: '机台编号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      search: true,
      selectOptions: machineNoList,
      clear: true,
      // 逻辑
      // 以下change停用eslint规则
      // eslint-disable-next-line
      change() {
        if (
          topFormModel.value.machineNo != '全自动丝印1' &&
          topFormModel.value.machineNo != '全自动丝印2' &&
          topFormModel.value.machineNo != '全自动打印2' &&
          topFormModel.value.machineNo != '全自动打印1' &&
          topFormModel.value.machineNo != '全自动打印3' &&
          topFormModel.value.machineNo != '全自动打印4' &&
          topFormModel.value.machineNo != '全自动打印5'
        ) {
          topFormModel.value.inkType = 'SIJ-J1000';
          inkTypeis.value = 'input';
        } else {
          if (topFormModel.value.machineNo == '全自动丝印1' || topFormModel.value.machineNo == '全自动丝印2') {
            inkTypeList.value = [
              {
                value: 'ZSR-150 ZM680D',
                label: 'ZSR-150 ZM680D'
              },
              {
                value: 'S-380E',
                label: 'S-380E'
              },
              {
                value: 'ZSR-150 SY',
                label: 'ZSR-150 SY'
              },
              {
                value: 'MRX-713J-A',
                label: 'MRX-713J-A'
              },
              {
                value: 'ZSR-150 ZM-480W',
                label: 'ZSR-150 ZM-480W'
              },
              {
                value: 'ZSR-150 ZM-680W',
                label: 'ZSR-150 ZM-680W'
              }
            ];
          } else {
            inkTypeList.value = [
              {
                value: 'SIJ-J1000',
                label: 'SIJ-J1000'
              },
              {
                value: 'JX-BWH002',
                label: 'JX-BWH002'
              }
            ];
          }
          topFormModel.value.inkType = null;
          inkTypeis.value = 'select';
        }
        if (
          topFormModel.value.machineNo == '全自动打印1' ||
          topFormModel.value.machineNo == '全自动打印2' ||
          topFormModel.value.machineNo == '全自动打印3' ||
          topFormModel.value.machineNo == '全自动打印4' ||
          topFormModel.value.machineNo == '全自动打印5' ||
          topFormModel.value.machineNo == '半自动打印2' ||
          topFormModel.value.machineNo == '半自动打印3'
        ) {
          printMode.value = 8;
          topFormModel.value.printMode = null;
          if (
            topFormModel.value.machineNo == '全自动打印1' ||
            topFormModel.value.machineNo == '全自动打印2' ||
            topFormModel.value.machineNo == '全自动打印3' ||
            topFormModel.value.machineNo == '全自动打印4' ||
            topFormModel.value.machineNo == '全自动打印5'
          ) {
            printModeList.value = [
              {
                value: '精细模式',
                label: '精细模式'
              },
              {
                value: '超精细薄',
                label: '超精细薄'
              },
              {
                value: '超精细中',
                label: '超精细中'
              },
              {
                value: '超精细厚',
                label: '超精细厚'
              }
            ];
          } else {
            printModeList.value = [
              {
                value: '16*2',
                label: '16*2'
              },
              {
                value: '16*3',
                label: '16*3'
              },
              {
                value: '22*2',
                label: '22*2'
              },
              {
                value: '22*3',
                label: '22*3'
              }
            ];
          }
        } else {
          printMode.value = 0;
          topFormModel.value.printMode = null;
        }
      }
    },

    {
      type: inkTypeis,
      key: 'inkType',
      label: '油墨类型',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: inkTypeList,
      clear: true
    },
    {
      type: 'input',
      key: 'inkLot',
      label: '油墨批次',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'faceNum',
      label: '面数',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'faceType',
      label: '面类型',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'faceA',
      label: 'A面',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'faceB',
      label: 'B面',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'periods',
      label: '周期',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'input',
      key: 'inkColor',
      label: '油墨颜色',
      span: 8,

      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },

    {
      type: 'select',
      key: 'printMode',
      label: '打印模式',
      span: printMode,
      // hidden: !printMode.value,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: printModeList
    },
    {
      type: '',
      span: printMode
    },
    {
      type: '',
      span: printMode
    },
    {
      type: 'radio',
      key: 'checkType1',
      label: '首件检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },
    {
      type: 'radio',
      key: 'checkType2',
      label: '自主检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },
    {
      type: 'radio',
      key: 'checkType3',
      label: '末件检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },
    {
      type: 'input',
      key: 'checkNum1',
      label: '首件检查数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'checkNum2',
      label: '自主检查数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'checkNum3',
      label: '末件检查数量',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'radio',
      key: 'shiftType',
      label: '班别',
      span: 6,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '白班',
          value: '0'
        },
        {
          label: '夜班',
          value: '1'
        }
      ]
    },

    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'operator',
      label: '操作员',
      defaultValue: authStore.userInfo.user.nickName || authStore.userInfo.user.userName,
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 8
    }
  ],
  modalHeight: '80vh',
  modalTableKey: 'badId',
  modalAddTableKey: 'badDoList',
  modalTableColumn: [
    {
      key: 'index',
      dataIndex: 'index',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'badName',
      dataIndex: 'badName',
      title: '检验项目',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'number',
      dataIndex: 'number',
      title: '缺陷数量',
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        if (record.disabled) {
          return <div style="min-width:150px">{record.number}</div>;
        }
        return (
          <Input
            style="height: 22px !important;"
            value={record.number}
            onBlur={e => {
              const value = e.target.value;
              if (!(isNonNegativeInteger(Number(value).toString()) && Number(value >= 0))) {
                message.error('请输入正整数');
                record.number = null;
                return;
              }
              record.number = Number(value);
            }}
          ></Input>
        );
      }
    }
  ],
  modleSearchUrlObj: {
    lotId: '/rcerp/agv/erp/getFaceType'
  },
  modalGetTableUrlSuffix: {
    add: 'selectBadId',
    byId: 'listBad'
  },
  modalClose() {
    lotType.value = 'input';
    moduleNoSearchData.value = null;
    lotids.value = [];
    inkTypeis.value = 'input';
    inkTypeList.value = [];
    printModeList.value = [];
    printMode.value = 0;
  }
};
